package cn.parllay.dingdangproject.utils.aliyun;

import android.util.Log;

import com.alibaba.sdk.android.push.CloudPushService;
import com.alibaba.sdk.android.push.CommonCallback;
import com.alibaba.sdk.android.push.noonesdk.PushServiceFactory;

import cn.parllay.dingdangproject.DingDangApplication;
import cn.parllay.dingdangproject.utils.SpUtils;

/**
 * Created by sunny on 2018/4/4.
 * 推送管理类
 */

public class PushManager {

    private static final String TAG = "push";
    private volatile static PushManager instance;

    private PushManager() {

    }

    public static PushManager getInstance() {

        if (instance == null) {
            synchronized (PushManager.class) {
                if (instance == null) {
                    instance = new PushManager();
                }
            }
        }
        return instance;
    }


    public void bindPush() {
        bindAccount();
        bindTag();
    }

    public void unBindPush() {
        unBindAccount();
        unBindTag();
    }

    //绑定账号
    public void bindAccount() {
        final String account = SpUtils.getInstance(DingDangApplication.getContext()).getString(SpUtils.MOBILE, "");
        PushServiceFactory.getCloudPushService().bindAccount(account, new CommonCallback() {
            @Override
            public void onSuccess(String s) {
                Log.e(TAG, "绑定账号:" + account + "成功");
            }

            @Override
            public void onFailed(String s, String s1) {
                Log.e(TAG, "绑定账号:" + account + "失败");
            }
        });
    }

    //解绑账号
    public void unBindAccount() {
        final String account = SpUtils.getInstance(DingDangApplication.getContext()).getString(SpUtils.MOBILE, "");
        PushServiceFactory.getCloudPushService().unbindAccount(new CommonCallback() {
            @Override
            public void onSuccess(String s) {
                Log.e(TAG, "解绑账号:" + account + "成功");
            }

            @Override
            public void onFailed(String s, String s1) {
                Log.e(TAG, "解绑账号:" + account + "失败");
            }
        });
    }

    //绑定标签
    public void bindTag() {
        final String[] tags = new String[]{SpUtils.getInstance(DingDangApplication.getContext()).getString(SpUtils.STORE_NO, "")};
        PushServiceFactory.getCloudPushService().bindTag(CloudPushService.DEVICE_TARGET, tags, null, new CommonCallback() {
            @Override
            public void onSuccess(String s) {
                Log.e(TAG, "绑定标签:" + tags[0] + "成功");
            }

            @Override
            public void onFailed(String s, String s1) {
                Log.e(TAG, "绑定标签:" + tags[0] + "失败");
            }
        });
    }

    //解绑标签
    public void unBindTag() {
        final String[] tags = new String[]{SpUtils.getInstance(DingDangApplication.getContext()).getString(SpUtils.STORE_NO, "")};
        PushServiceFactory.getCloudPushService().unbindTag(CloudPushService.DEVICE_TARGET, tags, null, new CommonCallback() {
            @Override
            public void onSuccess(String s) {
                Log.e(TAG, "解绑标签:" + tags[0] + "成功");
            }

            @Override
            public void onFailed(String s, String s1) {
                Log.e(TAG, "解绑标签:" + tags[0] + "失败");
            }
        });
    }

}
